#include <stdio.h>
#include <string.h>

#include "src/ltlog.h"

typedef int (*Run)(int argc, char const** argv);

int cat_Run(int argc, char const** argv);
int ls_Run(int argc, char const** argv);
int ini_show_Run(int argc, char const** argv);

typedef struct {
    char const* name;
    Run run;
} lt_Runner;

int
main(int argc, char const** argv) {
    lt_Runner runner[] = {
        {.name = "ls", .run = ls_Run},
        {.name = "cat", .run = cat_Run},
        {.name = "ini_show", .run = ini_show_Run},
        {.name = NULL},
    };

    for (lt_Runner const* it = runner; it->name != NULL; ++it) {
        if (strcmp(argv[0], it->name) == 0)
            return it->run(argc, argv);
    }

    int r = 0;
    if (argc > 1) {
        for (lt_Runner const* it = runner; it->name != NULL; ++it) {
            if (strcmp(argv[1], it->name) == 0)
                return it->run(argc - 1, argv + 1);
        }
        ltlog_Log1(Error, "invalid command", "command name:", String(argv[1]));
        r = 1;
    }

    for (lt_Runner const* it = runner; it->name != NULL; ++it) {
        printf("%s\n", it->name);
    }

    return r;
}
